/**
 * Created by Administrator on 15-10-20.
 */
var Core = RichBase.extend({
	DEFAULT_IMG : "http://www.12301.cc/images/defaultThum.jpg",
	LOADING_IMG : "http://www.12301.cc/images/icons/gloading.gif",
	PAYTYPE : {
		"0" : "账户余额支付",
		"1" : "支付宝支付",
		"2" : "授信支付",
		"3" : "产品自销",
		"4" : "现场支付",
		"5" : "微信支付"
	},
	PAYSTATUS : {
		0 : "景区到付",
		1 : "已成功",
		2 : "未支付"
	},
	ORDERSTATUS : {
		"0" : "未使用",
		"1" : "已使用",
		"2" : "已过期",
		"3" : "已取消",
		"4" : "凭证码被替代",
		"5" : "被终端撤销(已取消)",
		"6" : "被终端撤销(已使用)"
	},
	api : {
		//获取产品
		fetchProduct : "terminal_chk.html",
		//验证&搜索订单
		//terminal : "call/terminal.php",
		//2015-10-19改
		terminal : "call/terminal_dev.php"
	},
	isObjectEmpty : function(obj){
		var result = true;
		for(var i in obj){
			result = false;
			break;
		}
		return result;
	},
	getSalerid : function(){
		return "863886";
	},
	checkIsAdmin : function(){ return $("#isAdmin").val()},
	/**
	 * 获取远程产品(搜索)
	 * @param keyword  关键字   为空时默认获取全部产品
	 */
	fetchProduct : function(keyword,callbacks,sid){
		var that = this;
		var keyword = keyword || "";
		var api = this.api.fetchProduct;
		var callbacks = callbacks || {};
		var fn = new Function;
		var sid = sid || "";
		var loading = callbacks.loading || fn;
		var removeloading = callbacks.removeloading || fn;
		var timeout = callbacks.timeout || fn;
		var serverError = callbacks.serverError || fn;
		var empty = callbacks.empty || fn;
		var success = callbacks.success || fn;
		var xhr;
		xhr = PFT.Ajax({
			url : api,
			type : "get",
			dataType : "json",
			ttimeout : 15 * 1000,
			data : {
				"action" : "queryproduct",
				"keyword" : keyword,
				"sid" : sid
			},
			loading : function(){
				that.fire("onProductLoading");
				loading();
			},
			removeLoading : function(){
				that.fire("onProductRemoveLoading");
				removeloading();
			},
			timeout : function(res) {
				that.fire("onProductTimeout",res);
				timeout(res);
			},
			serverError : function(res) {
				that.fire("onProductServerError",res);
				serverError(res);
			}
		},function(res){
			if(res.status=="success"){
				if(res.list && res.list.length>0){
					that.fire("onProductSuccsee",res);
					success(res);
				}else{
					that.fire("onProductEmpty",res);
					empty(res);
				}
			}else{
				that.fire("onProductServerError",res);
				serverError(res);
			}
		})
		return xhr;
	},
	/**
	 * 验票
	 * @param btn  点击哪个按钮验票
	 * @param data 验票所需发送的所有数据 json字符串
	 */
	terminalCheck : function(data,opt){
		if(!data) return false;
		var that = this;
		var api = this.api.terminal;
		var fn = new Function;
		var opt = opt || {};
		var loading = opt.loading || fn;
		var removeLoading = opt.removeLoading || fn;
		var success = opt.success || fn;
		var fail = opt.fail || fn;
		var unlogin = opt.unlogin || fn;
		var timeout = opt.timeout || fn;
		var serverError = opt.serverError || fn;
		PFT.Ajax({
			url : api,
			type : "post",
			dataType : "json",
			data : {
				data : data
			},
			loading : function(){ loading()},
			removeLoading : function(){ removeLoading()},
			timeout : function(res){ timeout()},
			serverError : function(res){ serverError()}
		},function(res){
			if(res && res.status && res.status=="success"){
				that.success(res);
			}else if(res.status=="fail" && res.code && res.code==0){ //登录过期
				unlogin(res)
			}else{
				fail(res);
			}
		})
	},
	getQueryLoadingHtml : function(str){
		var str = str || "请求数据,请稍后...";
		return '<li class="sta loading" style="height:200px; line-height:200px; text-align:center"><img src="'+this.LOADING_IMG+'" alt=""/><span class="t">'+str+'</span></li>'
	},
	/**
	 * 前后端数据请求核心接口
	 * @param cmode(必须)     1:快速验证   2:改单验证   3:已过期订单验证  (当cmode为2或3时，只查询订单列表出来，还不做验证处理)
	 * @param salerid(必须)   商家编码
	 * @param voucher(必须)   输入的查询字段  凭证码&手机号&订单号    当cmode为3时，只能输入订单查询
	 */
	query : function(cmode,salerid,voucher,opt){
		var that = this;
		if(!cmode || !salerid || !voucher) return false;
		cmode = $.trim(cmode);
		salerid = $.trim(salerid);
		voucher = $.trim(voucher);
		var api = this.api.terminal;
		var fn = new Function;
		var opt = opt || {};
		var loading = opt.loading || fn;
		var removeLoading = opt.removeLoading || fn;
		var success = opt.success || fn;
		var empty = opt.empty || fn;
		var unlogin = opt.unlogin || fn;
		var fail = opt.fail || fn;
		var timeout = opt.timeout || fn;
		var serverError = opt.serverError || fn;
		var ajaxType = opt.ajaxType || "get";
		var data = {
			cmode : cmode,
			salerid : salerid,
			voucher : voucher
		};
		if(cmode==2 || cmode==3) data["query"] = 1;
		PFT.Ajax({
			url : api,
			type : ajaxType,
			dataType : "json",
			data : data,
			ttimeout : 60 * 1000,
			loading : function(){ loading()},
			removeLoading : function(){ removeLoading()},
			timeout : function(res){ timeout(res)},
			serverError : function(res){ serverError(res)}
		},function(res){
			if(res.status=="success"){
				var orders = res.orders;
				if(orders && !that.isObjectEmpty(orders)){
					success(res);
				}else{
					empty(res);
				}
			}else if(res.status=="fail" && res.code && res.code==0){
				unlogin(res);
			}else if(res.status=="fail" && res.msg){
				fail(res);
			}
		})
	},
	buildHtml : function(data){
		var tpl = $("#orderListTpl").html();
		var template = _.template(tpl);
		var html = template({data:data});
		return html;
	}
});

module.exports = Core;